package com.shide.common.util;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import lombok.experimental.UtilityClass;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yuwg
 */
@UtilityClass
public class AddressUtil {

    public Map<String, String> getAddress(String address) {
        // 省、市、区匹配
        String province = ReUtil.get(".*?(省|市|自治区|特别行政区)", address, 0);
        String city = ReUtil.get("(?:省|自治区|特别行政区)(.*?市)", address, 1);
        String district = ReUtil.get("(?:市|地区)(.*?区|.*?县|.*?市)", address, 1);

        // 去掉省市区信息后的详细地址
        String detailedAddress = address;
        if (StrUtil.isNotEmpty(province)) {
            detailedAddress = StrUtil.removePrefix(detailedAddress, province);
        }
        if (StrUtil.isNotEmpty(city)) {
            detailedAddress = StrUtil.removePrefix(detailedAddress, city);
        }
        if (StrUtil.isNotEmpty(district)) {
            detailedAddress = StrUtil.removePrefix(detailedAddress, district);
        }
        Map<String, String> map = new HashMap<>();
        map.put("province", province);
        map.put("city", city);
        map.put("district", district);
        map.put("detail", detailedAddress);
        return map;
    }

    public static void main(String[] args) {
        String address = "太原市北商新技广业开区钢隆店路35号华工业科技公司办公楼一层";
        System.out.println(getAddress(address));
    }
}
